### Packages
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
### 출력 영상 크기
plt.rcParams["figure.figsize"] = (16,9)
### 한글 표시
plt.rcParams['font.family'] = "Gulim" # 'AppleGothic' in mac
### 영상 출력 함수
def fn_imshow(img, axis='off'):
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
if axis=='off': plt.axis('off')
plt.show()
### 좌표 출력
def fn_mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDBLCLK:
print("Double click, [ x , y ]:[", x ,",", y,"]")
### 모자이크 처리 함수
def fn_mosaic (img, rect, size = 9) :
x1, y1, x2, y2 = rect
w = x2 - x1
h = y2 - y1
img_small = cv2.resize(img[y1:y2, x1:x2], (size, size))
img[y1:y2, x1:x2] = cv2.resize(img_small, (w, h), interpolation = cv2.INTER_AREA)
return img
### 영상 읽기
img_raw = cv2.imread(r'D:\image\lena.png')
fn_imshow(img_raw)
### 영상 출력 - Display an image in an OpenCV window
win_name = "Display window"
cv2.imshow(win_name, img_raw)
cv2.setMouseCallback(win_name, fn_mouse_callback)
cv2.waitKey(0)
cv2.destroyAllWindows()
Double click, [ x , y ]:[ 222 , 245 ] Double click, [ x , y ]:[ 350 , 387 ]
### 모자이크 처리
img_out = fn_mosaic(img_raw, [222 , 245, 350 , 387])
fn_imshow(img_out)